package com.fengye.common;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.*;
import java.util.Enumeration;
import java.util.List;

/**
 * ip获取工具类
 *
 * @Author fengye
 * @Date 2023/2/13 20:33
 **/
public class IPHelper {

    private final static Logger logger = LoggerFactory.getLogger(IPHelper.class);

    private static String  hostIp = StringUtils.EMPTY;

    static {
        String ip = null;
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface) allNetInterfaces.nextElement();
                List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
                for(InterfaceAddress address : interfaceAddresses) {
                    InetAddress inetAddress = address.getAddress();
                    if(inetAddress != null && inetAddress instanceof Inet4Address) {
                        if (StringUtils.equals(inetAddress.getHostAddress(), "127.0.0.1")) {
                            continue;
                        }
                        ip = inetAddress.getHostAddress();
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        hostIp = ip;
    }

    /**
     * 获取主机第一个有效ip
     *
     * 如果没有，返回空窜
     * @return
     */
    public static String getHostFirstIp() {
        return hostIp;
    }

    /**
     * 获取本机IP
     * 通过获取所有的networkInterface网络接口，然后遍历每个网络下的InterafceAddress组
     * 获得符合InetAddress instanceof Inet4Address 条件的一个IpV4地址
     * @return
     */
    public static String localIp() {
        return hostIp;
    }

    /**
     * 获取IP
     * @return
     */
    public static String getRealIp() {
        // 本地IP，如果没有配置外网IP则返回它
        String localIp = null;
        // 外网IP
        String netIp = null;

        try {
            Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            boolean finded = false;
            while (networkInterface.hasMoreElements() && !finded) {
                NetworkInterface ni = networkInterface.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();
                while (address.hasMoreElements()){
                    ip = address.nextElement();
                    if(!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && !ip.getHostAddress().contains(":")) {
                        netIp = ip.getHostAddress();
                        finded = true;
                        break;
                    } else if(ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && !ip.getHostAddress().contains(":")) {
                        localIp = ip.getHostAddress();
                    }
                }
            }

            if(StringUtils.isBlank(netIp)) {
                return localIp;
            }
            return netIp;
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
    }
}
